package com.eptok.gw.szcups.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.eptok.gw.szcups.cups.ErrorCode;
import com.eptok.gw.szcups.dao.JdbcDao;
import com.eptok.gw.szcups.dao.bo.Cupsreveral;
import com.eptok.gw.szcups.pojo.Reversal;
import com.eptok.gw.szcups.service.ReversalService;

public class ReveralProcess extends Thread{
	
	private Cupsreveral cupsreveral;
	protected static final Logger log = LoggerFactory.getLogger(ReveralProcess.class);
	
	
	public ReveralProcess(Cupsreveral cupsreveral){
		this.cupsreveral = cupsreveral;
	}

	public void run(){
		log.info("[start]冲正流水号为"+cupsreveral.getReqtraceId()+"的交易");
		JdbcDao jdbcDao = (JdbcDao)ContextLoader.getCurrentWebApplicationContext().getBean("jdbcDao");
		Cupsreveral reveral = new Cupsreveral();
		reveral.setId(cupsreveral.getId());
		reveral.setRetryCount(cupsreveral.getRetryCount()+1);
		reveral.setStatus("1");
		jdbcDao.updateCupsreveral(reveral);
		ReversalService reversalService = new ReversalService();
		Reversal reversal = new Reversal();
		reversal.setOldReqSerialNumber(cupsreveral.getReqtraceId());
		try {
			reversalService.process(reversal);
			if(reversal.getStatus().equals("00")){
				reveral.setStatus("2");
			}else{
				reveral.setStatus("3");
			}
		} catch (Exception e) {
			log.error("冲正异常",e);
			reveral.setStatus("3");
		}
		log.info("[end]冲正流水号为"+cupsreveral.getReqtraceId()+"的交易，处理结果:"+reveral.getStatus()+" 描述:"+ErrorCode.map.get(reveral.getStatus()));
		jdbcDao.updateCupsreveral(reveral);
	}
}
